#include "stdafx.h"
#include "srvmain.h"

#ifdef _DEBUG
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#undef DEBUG_NEW
	#define	DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__)
	#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
class rds_remote*__rdsdata__[__max_rdsdata__]={NULL};
/////////////////////////////////////////////////////////////////////////////
BOOL service_main::load_rdsdata(LPTSTR strError)
{
	_bstr_t bstrName;
	TCHAR szKey[16]={0},szData[MAX_PATH]={0};
	long i(0),lCount(GetPrivateProfileInt(TEXT("rdsdata"),TEXT("count"),0,__iniConfig__));
	lCount=max(0,min(lCount,__max_rdsdata__));
	for(;i<lCount;++i)
	{
		wsprintf(szKey,_T("rdsstamp%d"),i);
		GetPrivateProfileString(TEXT("rdsdata"),szKey,TEXT(""),szData,32,__iniConfig__);
		if(!(__rdsdata__[i]=new rds_remote(szData)))
		{
			lstrcpy(strError,TEXT("error new rds_remote"));
			return(FALSE);
		}
		wsprintf(szKey,_T("rdsmemo%d"),i);
		GetPrivateProfileString(TEXT("rdsdata"),szKey,TEXT(""),szData,64,__iniConfig__);
		lstrcpynA(__rdsdata__[i]->scriptModule,bstrName=szData,sizeof(__rdsdata__[i]->scriptModule));

		wsprintf(szKey,_T("rdsfreq%d"),i);
		__rdsdata__[i]->_rdsfreq = GetPrivateProfileInt(TEXT("rdsdata"),szKey,60,__iniConfig__);
		__rdsdata__[i]->_rdsfreq = max(__rdsdata__[i]->_rdsfreq,15);

		wsprintf(szKey,_T("rdsname%d"),i);
		GetPrivateProfileString(TEXT("rdsdata"),szKey,TEXT(""),szData,32,__iniConfig__);
		lstrcpynA(__rdsdata__[i]->nameModule,bstrName=szData,sizeof(__rdsdata__[i]->nameModule));

		wsprintf(szData,TEXT("%srdssql\\%s.sql"),__main_home__,(LPCTSTR)bstrName);
		if(!__rdsdata__[i]->load_sqlfile(szData))
		{
			wsprintf(strError,TEXT("error load %s.sql"),(LPCTSTR)bstrName);
			return(FALSE);
		}
		if(!__rdsdata__[i]->readrds(__trade_date__))
		{
			wsprintf(strError,TEXT("error read data : %s.sql"),(LPCTSTR)bstrName);
			return(FALSE);
		}
		__rdsdata__[i]->_rdsreadtime = __rdsdata__[i]->_rdsfreq;
	}
	return(TRUE);
}

void service_main::check_rdsdata(void)
{
	for(long i=0,j=0;i<__max_rdsdata__&&__rdsdata__[i];++i)
	{
		if(--__rdsdata__[i]->_rdsreadtime>0) continue;
		__rdsdata__[i]->_rdsreadtime = __rdsdata__[i]->_rdsfreq;
		try
		{
			__rdsdata__[i]->readrds(__trade_date__);
		}
		catch(...)
		{
		}
	}
}

void service_main::reget_rdsdata(void)
{
	for(long i=0,j=0;i<__max_rdsdata__&&__rdsdata__[i];++i)
	{
		try
		{
			__rdsdata__[i]->readrds(__trade_date__);
		}
		catch(...)
		{
		}
	}
}

void service_main::release_rdsdata(void)
{
	for(long i=0;i<__max_rdsdata__;++i)
	{
		if(__rdsdata__[i])
		{
			delete __rdsdata__[i];
			__rdsdata__[i] = NULL;
		}
	}
}
